/***************************************************************************
 *  Filename: mastercommunicator.h
 *  Class: mastercommunicator
 *
 *  Copyright 2012  Xander van Kooten
 *  <xandervk@gmail.com>
 *
 *  Copyright 2012  Mario Henrique Voorsluys
 *  <forcaeluz@gmail.com>
 *
 ****************************************************************************/

/*
 *
 * This file is part of Parsax-Remote-Controller.
 *
 *    Parsax-Remote-Controller is free software: you can redistribute it
 *    and/or modify it under the terms of the GNU General Public License
 *    as published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    Parsax-Remote-Controller is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Parsax-Remote-Controller.
 *    If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MASTERCOMMUNICATOR_H
#define MASTERCOMMUNICATOR_H

#include "CCC-Communication_global.h"

#include <QObject>
#include <QTcpSocket>
#include <QHostAddress>

/*!
 \brief Handles the communication from the master side.

 The MasterCommunicator is responsible for connecting to the slave,
 and handling the network data at the master side. This includes sending
 and receiving data.

 Besides that, the communicator should inform the connection status when changed,
 or on request. This allows the user interface to react on the connection changes.

*/
class CCCCOMMUNICATIONSHARED_EXPORT MasterCommunicator : public QObject {
    Q_OBJECT
public:
    /*!
     \brief Object constructor.

     \param parent
    */
    explicit MasterCommunicator(QObject *parent=0);
    /*!
     \brief Object destructor.

    */
    ~MasterCommunicator();
    /*!
     \brief Tries to establish a connection with the slave.

     \param address Address to connect to.
     \param port Port to connect.
    */
    void connectToSlave(QString address, int port);
    /*!
     \brief Close the connection with the slave.

    */
    void closeConnection();
    /*!
     \brief Send data to the slave.

     \param data Data to be sent.
    */
    void sendData(QByteArray data);


    /*!
     \brief To monitor connection status.

     \return bool True if connected, false otherwise.
    */
    bool isConnected();
signals:
    /*!
     \brief Emmited when a valid message is received.

     \param message The content of the message.
    */
    void messageReceived(QString message);
    /*!
     \brief Emmited when the connection with the slave is lost.

    */
    void connectionLost();

    /*!
     \brief Emmited when the connection with the slave is established.

    */
    void connectionEstablished();
public slots:

private:
    QTcpSocket *tcpSocket; /*!< The socket for the connection between master and slave. */

private slots:
    /*!
     \brief Read data from the socket.

    */
    void receiveMessage();
    /*!
     \brief Handle disconnet events.

    */
    void handleDisconnect();
};

#endif // MASTERCOMMUNICATOR_H
